﻿/*********************************************************************************
 * Copyright(c)  	2012 @kaleyroy
 * 创 建 者：		@kaleyroy 
 * 电子邮箱:		kaleyroy@gmail.com
**********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Collections;

using USS.Domain;
using USS.Generic.Logging;
using USS.Generic.IException;
using USS.Service;
using USS.Service.DataObject;
using USS.Service.ValueObject;


/// <summary>
/// 
/// 修改记录:
///	
/// </summary>
namespace USS.Web.Controllers
{
	/// <summary>
    /// 级别控制器对象
    /// </summary>
    public class LevelController : BaseController
    {
        /// <summary>
        /// 级别服务实例
        /// </summary>
        public ILevelService ServLevel { get; set; }
        
        /// <summary>
        /// 新建级别
        /// </summary>
        /// <param name="levelData">级别数据对象</param>
        /// <returns>JRSMessage</returns>
        public ActionResult New(LevelData levelData)
        {
            try
            {
                if (null == levelData)
                    throw new InvalidParameterException("无效级别信息!");
                ServLevel.New(levelData);
                msg.Result = Constants.SUCCESS;
            }
            catch (Exception ex)
            {
                msg.Result = Constants.ERROR;
                if (ex is InvalidParameterException)
                    msg.Result = Constants.INVALID;
                Loggers.ErrorLog.Error("新建级别异常!", ex);
            }
            return Json(msg);
        }

        /// <summary>
        /// 编辑级别
        /// </summary>
        /// <param name="levelData">级别数据对象</param>
        /// <returns>JRSMessage</returns>
        public ActionResult Edit(LevelData levelData)
        {
            try
            {
                if (null == levelData)
                    throw new InvalidParameterException("无效级别信息!");
                ServLevel.Edit(levelData);
                msg.Result = Constants.SUCCESS;
            }
            catch (Exception ex)
            {
                msg.Result = Constants.ERROR;
                if (ex is InvalidParameterException)
                    msg.Result = Constants.INVALID;
                Loggers.ErrorLog.Error("编辑级别异常!", ex);
            }
            return Json(msg);
        }

        /// <summary>
        /// 启用/禁用级别
        /// </summary>
        /// <param name="levelID">级别标识</param>
        /// <param name="flag">启用/禁用状态</param>
        /// <returns>JRSMessage</returns>
        public ActionResult Enable(int levelID = 0, int flag = -1)
        {
            try
            {
                if (levelID <= 0 || flag <= -1)
                    throw new InvalidParameterException("无效参数信息!");
                ServLevel.Enable(levelID,flag);
                msg.Result = Constants.SUCCESS;
            }
            catch (Exception ex)
            {
                msg.Result = Constants.ERROR;
                if (ex is InvalidParameterException)
                    msg.Result = Constants.INVALID;
                string prefix = flag == 0 ? "禁用" : "启用";
                Loggers.ErrorLog.Error(prefix + "级别异常!", ex);
            }
            return Json(msg);
        }

        /// <summary>
        /// 删除级别
        /// </summary>
        /// <param name="levelID">级别标识</param>
        /// <returns>JRSMessage</returns>
        public ActionResult Delete(int levelID = 0)
        {
            try
            {
                if (levelID <= 0)
                    throw new InvalidParameterException("无效参数信息!");
                ServLevel.Delete(levelID);
                msg.Result = Constants.SUCCESS;
            }
            catch (Exception ex)
            {
                msg.Result = Constants.ERROR;
                if (ex is InvalidParameterException)
                    msg.Result = Constants.INVALID;
                Loggers.ErrorLog.Error("删除级别异常!", ex);
            }
            return Json(msg);
        }

        /// <summary>
        /// 级别列表
        /// </summary>
        /// <param name="levelName">级别名称</param>
        /// <param name="page">当前页码</param>
        /// <param name="rows">页面大小</param>
        /// <returns>JESDataGrid</returns>
        public ActionResult List(string levelName = "",int page = -1,int rows = -1)
        {
            int total = 0;
            List<LevelData> dLevels = new List<LevelData>();
            JESDataGrid<LevelData> grid = new JESDataGrid<LevelData>();
            try
            {
                if(page <= -1 || rows <= -1)
                    throw new InvalidParameterException("无效参数信息!");                
                List<Level> levels = ServLevel.AllLevels(levelName, page, rows,out total);
                foreach (Level level in levels)
                {
                    LevelData dLevel = new LevelData();
                    dLevel.FromEntity(level);
                    dLevels.Add(dLevel);
                }
                grid.total = total;
                grid.rows = dLevels;
            }
            catch (Exception ex)
            {
                msg.Result = Constants.ERROR;
                if (ex is InvalidParameterException)
                    msg.Result = Constants.INVALID;
                Loggers.ErrorLog.Error("查询级别异常!", ex);                
            }
            return Json(grid);
        }

        /// <summary>
        /// 有效级别列表
        /// </summary>
        /// <returns>JESLevelOption</returns>
        public ActionResult EnabledList()
        {
            List<JESLevelOption> options = new List<JESLevelOption>();
            try
            {
                List<Level> levels = ServLevel.EnabledLevels();
                foreach (Level level in levels)
                {
                    JESLevelOption option = new JESLevelOption();
                    option.id = "" + level.LevelID;
                    option.text = level.LevelName;
                    option.color = "#" + level.LevelColor;
                    options.Add(option);
                }
            }
            catch (Exception ex)
            {
                msg.Result = Constants.ERROR;
                Loggers.ErrorLog.Error("查询级别异常!", ex);
            }
            return Json(options);
        }
    }
}
